package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.domain.SmsHomeRecommendProduct;
import com.itheima.service.SmsHomeRecommendProductService;
import com.itheima.util.PageBean;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")
public class SmsHomeRecommendProductController {

    @DubboReference
    private SmsHomeRecommendProductService recommendProductService;

    @GetMapping("/home/recommendProduct/list")
    public R list(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        PageBean pageBean = recommendProductService.list(pageNum, pageSize, productName, recommendStatus);
        return R.success(pageBean);
    }

    @PostMapping("/home/recommendProduct/create")
    public R add(@RequestBody List<SmsHomeRecommendProduct> recommendProducts) {
        int res = recommendProductService.create(recommendProducts);
        if (res > 0) {
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }

    @PostMapping("/home/recommendProduct/delete")
    public R delete(@RequestParam("ids") List<Long> ids) {
        int res = recommendProductService.delete(ids);
        if (res > 0) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    @PostMapping("/home/recommendProduct/update/recommendStatus")
    public R updateStatus(@RequestParam("ids") List<Long> ids, Integer recommendStatus) {
        int res = recommendProductService.updateStatus(ids, recommendStatus);
        if (res > 0) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    @PostMapping("/home/recommendProduct/update/sort/{id}")
    public R updateSort(Integer sort, Integer id) {
        int res = recommendProductService.updateSort(sort, id);
        if (res > 0) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

}
